/* 
 * File:   commons.h
 * Author: Administrator
 *
 * Created on 2013?8?13?, ??8:56
 */

#ifndef COMMONS_H
#define	COMMONS_H

#ifdef	__cplusplus
extern "C" {
#endif
#ifdef	__cplusplus
}
#endif

#include "p30f6010A.h"
#include <spi.h>
#include <pwm.h>
#include <ports.h>
#include <stdlib.h>
#include <stdio.h>
#include <timer.h>
#include <time.h>
#include <uart.h>
#include <xlcd.h>
#include <string.h>



#define PCBGen "1Gen"
#define FCY  8000000				// 8Mhz crystal in 4X PLL mode

#define MILLISEC FCY/8000			// 1 mSec delay constant
#define uSEC FCY/8000/1000

#define BAUDRATE 9600
#define TMR_RATE	FCY/64			// Timer count rate with 1:64 prescaler
#define HALF_SEC	TMR_RATE/2		// Timer count value for 0.5 second
#define SEC	TMR_RATE


//???

#define S1 !PORTDbits.RD12      //S6???? S1--S5???, new board definition
#define S2 !PORTDbits.RD13
#define S3 !PORTFbits.RF0
#define S4 !PORTFbits.RF1
#define S5 !PORTGbits.RG1
//S6??????MCLR

// output of BNC port
#define Out1 PORTBbits.RB12	// lick (1ms) or come (2ms)
#define Out2 PORTBbits.RB13	// odor A on (1ms) and off (2ms)
#define Out3 PORTBbits.RB14	// odor B on (1ms) and off (2ms)
#define Out4 PORTBbits.RB15	// normal water on (1 ms) or off (2ms), bitter water on (3ms) or off (4ms)
#define Out5 PORTDbits.RD14	// there are conflicts , maybe with LCD for 5 and 6
#define Out6 PORTDbits.RD15	//
#define Out7 PORTDbits.RD14	// there are conflicts , maybe with LCD for 5 and 6
#define Out8 PORTDbits.RD15	//



//#define LED_GREEN   PORTAbits.RA15 //???????? new board definition
//#define LED_RED     PORTAbits.RA14 //???????? new board definition


// it seems that RD8 and RD9 could be the come detectors

//#define Come 1 // Head fixed, always come
//#define Come2 1 // Head fixed, always come

// it seems that RD10 and RD11 could be the lick detectors
#define LickOnPin        !PORTDbits.RD10 // mice ??? P10? ??D29?

//LCD
#define RS	LATFbits.LATF8 //1-data?0-command
#define RW	LATFbits.LATF2 //1-Read,0-Write
#define E	LATFbits.LATF3//?????

#define DATA	LATD
#define TRIS_DATA  TRISD

#define  RW_TRIS	TRISFbits.TRISF2
#define  RS_TRIS	TRISFbits.TRISF8
#define  E_TRIS		TRISFbits.TRISF3


// LCD fuction , same as before
#define set_function()	lcd_cmd( 0x38 ) //8bit, 5*8, 1/16DUTY
#define cursor_right()  lcd_cmd( 0x14 )//????
#define cursor_left()   lcd_cmd( 0x10 )//????
#define display_shift() lcd_cmd( 0x1C )//????
#define home_clr()      lcd_cmd( 0x01 ) //???
#define home_it()       lcd_cmd( 0x02 ) //????
#define line_2()        lcd_cmd( 0xC0 ) // (0xC0)??????

//#define TRUE 1			// TO TEST: iComeVar == TRUE
//#define FALSE 0

//??????????  ????8??, ??????????????????
#define  water_sweet    1    //pwm1L		//same as old definition
#define  odor_A         2    //pwm1H		//same as old definition
#define  odor_B         3    //pwm2l		//same as old definition
#define  water_bitter   4    //pwm2h		//same as old definition
#define  odor_C         5
#define  odor_D         6



// serial port

//  define status: valve
// every status change must be registered in the send word, otherwise it will be incorrect
// first word is for licking and coming, lick: 0x01; not lick: 0x00
// position pointer cannot be 0x55 and  0xAA?
#define SpLick 	 0 	//        !PORTDbits.RD8  // ???????P2??????P4???D27?
#define SpCome 	 1  	// Head fixed, always come
#define SpLick2	 2   // lick2    //!PORTDbits.RD9  // ???????P6??????P7???D28?#define Come 1
#define SpCome2	 3   	// Head fixed, always come
// the second word is for performance, false alarm,
#define SpFalseAlarm		 4	// false alarm
#define SpCorrectRejection		 5	// correct rejection
#define SpMiss		 6	// miss
#define SpHit		 7 	// hit
// the third word is for the first set of valves
#define SpWater_sweet	 8     // 1    //pwm1L		//same as old definition
#define SpOdor_A             9         // #define  2    //pwm1H		//same as old definition
#define SpOdor_B	10    //      3    //pwm2l		//same as old definition
#define SpWater_bitter	 11   // 4    //pwm2h		//same as old definition
// the fourth word is for the second set of vales
#define SpValve5	12
#define SpValve6 	13
#define SpValve7 	14
#define SpValve8 	15  // overall eight valves in the current design, may be more later


// the fifth word is for the output value for
#define SpOut1		16  // PORTBbits.RB12	// lick (1ms) or come (2ms)
#define SpOut2	 	17  // PORTBbits.RB13	// odor A on (1ms) and off (2ms)
#define SpOut3	 	18  // PORTBbits.RB14	// odor B on (1ms) and off (2ms)
#define SpOut4		19 // PORTBbits.RB15	// normal water on (1 ms) or off (2ms), bitter water on (3ms) or off (4ms)


//press key, s1s1 through s5s5
#define SpS1S1 	 20
#define SpS1S2   21
#define SpS1S3   22
#define SpS1S4	 23
#define SpS1S5   24
#define SpS2S1 	 25
#define SpS2S2   26
#define SpS2S3   27
#define SpS2S4	 28
#define SpS2S5   29
#define SpS3S1 	 30
#define SpS3S2   31
#define SpS3S3   32
#define SpS3S4	 33
#define SpS3S5   34
#define SpS4S1 	 35
#define SpS4S2   36
#define SpS4S3   37
#define SpS4S4	 38
#define SpS4S5   39
#define SpS5S1 	 40
#define SpS5S2   41
#define SpS5S3   42
#define SpS5S4	 43
#define SpS5S5   44


#define SpStepN         51
#define SpSessN         52
#define SptrialNperSess 49
#define SpSessHit       45
#define SpSessMiss      46
#define SpSessFA	    47
#define SpSessCR        48
#define SpSessCorrRate 	50

#define SpFirOdor       53
#define SpFirOdorL      54
#define SpOdorDelay     55
#define SpSecOdor       56
#define SpSecOdorL      57
#define Sptrialtype     58
#define SpITI           59  // 1 start 0 end
#define SpproLpun       60
#define SpSess          61  // 1 start 0 end
#define SpTrain         62  // 1 start 0 end
#define Splaser         65

#define SpOdor_C        66
#define SpOdor_D        64

#define SpHit2         67
#define SpMiss2           68
#define SpFalseAlarm2         69
#define SpCorrectRejection2         70

#define SpHit3         71
#define SpMiss3          72
#define SpFalseAlarm3         73
#define SpCorrectRejection3        74


// size 16, words to be sent; if want to send other words, add more here
// every message will include position of pointer, that is to say the status to be changed, and the value to be changed to



void Init_LCD(void); // lcd???
void Init_PWM(void);
void InitUART2(void);
void InitTMR(void);
//void InitTMR1(void); // ?lcd.c???
//??/?
void Valve_ON(unsigned int N, unsigned int rate); //????rate???N??
void Valve_OFF(unsigned int N);

void Digt_out(unsigned int Nchan, unsigned int Duration); // digital output in 1
void NewDigt_out(unsigned int Nchan, float Duration);

void lcd_cmd(char cmd); // ????lcd
void lcd_data(char data); // ????lcd
void puts_lcd(unsigned char *data, unsigned char count); //?????
void Mode_Show(unsigned int i);
void LCD_set_xy(unsigned int x, unsigned int y); //?lcd.c???
void counts_dispose(unsigned int counter); //????1000???,??????????????

void DelayNmSec(unsigned int N); //ms     ????
void DelayNuSec(unsigned int N); //us
void DelayNmSecNew(unsigned int N); //ms HouHan
// serial port
void UART2PutChar(unsigned char Ch);



void __attribute__((__interrupt__, no_auto_psv)) _T2Interrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _T1Interrupt(void); // return to main if mouse left port
void __attribute__((__interrupt__, no_auto_psv)) _ADCInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _PWMInterrupt(void);
void __attribute__((__interrupt__, no_auto_psv)) _U2TXInterrupt(void);


void Puts_UART2(unsigned char *data, unsigned char count);
void localSendOnce(int thisSp, int thisValue);
void ErrorUartSend(void);

void SetupPorts(void); //????
void beep(void);

extern char serialPortString[];
extern unsigned int period; //pwm
extern unsigned int timerCounterI, timerCounterJ;
extern unsigned int ones, tens, hunds, thous, ten_thous, hund_thous, thous_thous;


unsigned int getFuncNumber(int targetDigits, char* hint);
void LCDWriteString(char* s);
#endif	/* COMMONS_H */
